package com.yaosheng.thread.map;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * Created by yaosheng on 2021/7/5.
 * 阻塞队列
 */
public class TestLinkedBlockingQueue {

    static LinkedBlockingQueue<String> strs = new LinkedBlockingQueue<> ();
    static Random r = new Random ();

    public static void main(String[] args) {

        new Thread( () -> {
            for(int i = 0;i < 100;i ++){
                try{
                    strs.put ("a" + i);   //如果满了，就会等待
                    TimeUnit.SECONDS.sleep (r.nextInt (10));
                } catch (InterruptedException e) {
                    e.printStackTrace ();
                }
            }
        },"p1").start ();

        for(int i = 0;i < 5;i ++){
            new Thread ( () -> {
                for(;;){
                    try{
                        System.out.println (Thread.currentThread ().getName () + "take -" +strs.take ());
                    }catch (InterruptedException e){
                        e.printStackTrace ();
                    }
                }
            },"c" + i).start ();
        }
    }
}
